﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Test_QianBZ.Models;
using Test_QianBZ.DataAccess;

namespace Test_QianBZ.Repository
{
    public class FoodRepository
    {
        public List<FoodModel> GetMenu()
        {
            var db = new eatContext();
            var menu = db.TbMenu
                .Select(s => new FoodModel
                {
                    FoodId = s.FoodId,
                    FoodName = s.FoodName
                })
                .ToList();
            return menu;
        }

        public FoodModel GetFood(int foodId)
        {
            var db = new eatContext();
            var food = db.TbMenu
                .Where(s => s.FoodId == foodId)
                .Select(s => new FoodModel
                {
                    FoodId = s.FoodId,
                    FoodName = s.FoodName
                })
                .FirstOrDefault();
            return food;
        }

        public int AddFood(string foodName)
        {
            var db = new eatContext();
            var tbmenu = new TbMenu
            {
                FoodName = foodName
            };
            db.TbMenu.Add(tbmenu);
            return (db.SaveChanges());
        }

        public int EditFood(FoodModel food)
        {
            var db = new eatContext();
            var f = db.TbMenu.Where(s => s.FoodId == food.FoodId).FirstOrDefault();
            if (f != null)
            {
                f.FoodName = food.FoodName;
                return db.SaveChanges();
            }
            else
            {
                return 0;
            }

        }
    }
}
